package com.example.leechyli.crashmonitor;

import android.app.Activity;
import android.app.Application;
import android.os.Bundle;
import android.util.Log;

import java.util.ArrayList;

import monitor.MonitorUtil;

/**
 *
 * Created by leechyli on 2017/7/14.
 */
public class MyApplication extends Application {

    ArrayList<Activity> list = new ArrayList<Activity>();

    @Override
    public void onCreate() {
        super.onCreate();


       /* Bugly SDK初始化
        * 参数1：上下文对象
        * 参数2：APPID，平台注册时得到,注意替换成你的appId
        * 参数3：是否开启调试模式，调试模式下会输出'CrashReport'tag的日志
        */
//        CrashReport.initCrashReport(getApplicationContext(), "900029763", true);
        Log.w(MonitorUtil.TAG, "Application create.");
        MonitorUtil.initial(this,true);
    }

//    public void init(){
//        //设置该CrashHandler为程序的默认处理器
//        UnCeHandler catchExcep = new UnCeHandler(this);
//        Thread.setDefaultUncaughtExceptionHandler(catchExcep);
//    }

//    /**
//     * Activity关闭时，删除Activity列表中的Activity对象*/
//    public void removeActivity(Activity a){
//        list.remove(a);
//    }
//
//    /**
//     * 向Activity列表中添加Activity对象*/
//    public void addActivity(Activity a){
//        list.add(a);
//    }

    /**
     * 关闭Activity列表中的所有Activity*/
    public void finishActivity(){
//        android.os.Process.killProcess(android.os.Process.myPid());
//        return ;
//        for (Activity activity : list) {
//            if (null != activity) {
//                activity.finish();
//            }
//        }
        //杀死该应用进程
//        android.os.Process.killProcess(android.os.Process.myPid());
    }

    private void registerActivityListener() {
        registerActivityLifecycleCallbacks(new Application.ActivityLifecycleCallbacks() {
            @Override
            public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
                /**
                 *  监听到 Activity创建事件 将该 Activity 加入list
                 */
                //list.add(activity);

            }

            @Override
            public void onActivityStarted(Activity activity) {

            }

            @Override
            public void onActivityResumed(Activity activity) {

            }

            @Override
            public void onActivityPaused(Activity activity) {

            }

            @Override
            public void onActivityStopped(Activity activity) {

            }

            @Override
            public void onActivitySaveInstanceState(Activity activity, Bundle outState) {

            }

            @Override
            public void onActivityDestroyed(Activity activity) {
//                if (null==list||list.isEmpty()){
//                    return;
//                }
//                if (list.contains(activity)){
//                    /**
//                     *  监听到 Activity销毁事件 将该Activity 从list中移除
//                     */
//                    list.remove(activity);
//                }
            }
        });
    }
}
